package com.kzd.sdk.ui.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.webkit.JavascriptInterface;

import com.kzd.sdk.core.KZD_ApiManager;
import com.kzd.sdk.core.KZD_SdkController;
import com.kzd.sdk.ui.base.WebViewActivity;

import static com.kzd.sdk.core.KZD_ApiConstance.KEY_WEB_VIEW_URL;

public class KZD_WebPayActivity extends WebViewActivity {

    public static void open(String url) {
        Activity activity = KZD_SdkController.getActivity();
        if (activity == null) {
            return;
        }
        Intent intent = new Intent(activity, KZD_WebPayActivity.class);
        intent.putExtra(KEY_WEB_VIEW_URL, url);
        activity.startActivity(intent);
    }

    private String mOrderId;

    @SuppressLint("AddJavascriptInterface")
    @Override
    public void initView() {
        mWebView.addJavascriptInterface((this), ("android"));
    }

    @Override
    public void initData() {

        setTextViewTitle("支付");

        String url = getIntent().getStringExtra(KEY_WEB_VIEW_URL);
        if (TextUtils.isEmpty(url)) {
            return;
        }
        mWebView.loadUrl(url);
    }

    @Override
    public void onBackPressed() {
        if (TextUtils.isEmpty(mOrderId)) {
            KZD_SdkController.getOnPayListener().onPayingError("取消支付");
        } else {
            KZD_ApiManager.httpGamePayOrder(mOrderId);
        }
        super.onBackPressed();
    }

    @JavascriptInterface
    public void getOlder_id(String older_id) {
        this.mOrderId = older_id;
    }

    @JavascriptInterface
    public void finshActivy() {
        toast("支付未开通！");
        KZD_SdkController.getOnPayListener().onPayingError("未开通支付");
        finish();
    }

    @JavascriptInterface
    public void closeActivity() {
        KZD_ApiManager.httpGamePayOrder(mOrderId);
        finish();
    }

}
